package com.fernfx.xingtan.my.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnScrollListener;
import android.view.View;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.my.adapter.DiamondsHistroyInfoAdapter;
import com.fernfx.xingtan.my.contract.MyDiamondsContract;
import com.fernfx.xingtan.my.entity.DiamondsHistroyInfoEntity;
import com.fernfx.xingtan.my.entity.MyDiamondsEntity;
import com.fernfx.xingtan.my.presenter.MyDiamondsPresenter;
import com.fernfx.xingtan.utils.CollectionUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by admin on 2018/8/16.
 * 我的钻石
 */

public class MyDiamondsActivity extends BaseActivity implements MyDiamondsContract.View, SwipeRefreshLayout.OnRefreshListener {
    @BindView(R.id.my_diamonds_count_tv)
    TextView myDiamondsCountTv;
    @BindView(R.id.diamond_histroy_info_srlat)
    SwipeRefreshLayout diamondHistroyInfoSrlat;
    @BindView(R.id.no_diamonds_histroy_toast_tv)
    TextView noDiamondsHistoryToastTv;
    @BindView(R.id.diamond_histroy_info_syrw)
    RecyclerView diamondHistroyInfoSyrw;

    //presenter
    private MyDiamondsContract.Presenter presenter;
    //list
    private List<DiamondsHistroyInfoEntity.ObjBean.RecordsBean> recordsBeansList;
    //当前页码
    private int pageNo = 1;
    private final int pageSize = 10;
    //adapter
    private DiamondsHistroyInfoAdapter diamondsHistroyInfoAdapter;
    //是否清空list
    private boolean clearFlag = false;
    private LinearLayoutManager linearLayoutManager;

    Handler handlerForRefresh = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x93: {
                    //结束刷新
                    diamondHistroyInfoSrlat.setRefreshing(false);
                }
            }
        }
    };

    public static void start(Context context) {
        Intent intent = new Intent(context, MyDiamondsActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_my_purse_diamonds;
    }

    @Override
    protected void initView() {
        //init presenter
        presenter = new MyDiamondsPresenter();
        presenter.init(this);
        //设置标题
        setToolbarTitle("钻石");
        diamondHistroyInfoSrlat.setColorSchemeResources(
                R.color.colorPrimary,
                R.color.green,
                R.color.red
        );
    }

    @Override
    protected void initData() {
        //请求钻石
        requestArgsMap.clear();
        presenter.getDiamondTotalAmount(requestArgsMap);
        //记录
        loadDiamondList(pageNo, pageSize);
        linearLayoutManager = new LinearLayoutManager(this);
        diamondHistroyInfoSyrw.setLayoutManager(linearLayoutManager);
        diamondHistroyInfoSyrw.addItemDecoration(new MyDiamondsPresenter.SpaceItemDecoration(0, 23));
        //init adapter
        //设置adapter
        diamondsHistroyInfoAdapter = new DiamondsHistroyInfoAdapter(mContext);
        recordsBeansList = new ArrayList<>(0);
        diamondsHistroyInfoAdapter.setDatas(recordsBeansList);
        diamondHistroyInfoSyrw.setAdapter(diamondsHistroyInfoAdapter);
    }

    @Override
    protected void initListener() {
        diamondHistroyInfoSrlat.setOnRefreshListener(this);

        //下拉加载更多
        diamondHistroyInfoSyrw.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (null == diamondsHistroyInfoAdapter) {
                    return;
                }
                int lastItemPosition = linearLayoutManager.findLastVisibleItemPosition();
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastItemPosition + 1 == diamondsHistroyInfoAdapter.getItemCount()) {
                    if (clearFlag) {
                        clearFlag = false;
                    }
                    loadDiamondList(pageNo, pageSize);
                    ToastUtil.showBottomtoast("正在加载下一页");
                }
            }
        });
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }

    @Override
    public void showDiamondsInfo(MyDiamondsEntity myDiamondsEntity) {
        MyDiamondsEntity.ObjBean objBean = myDiamondsEntity.getObj();
        if (null == objBean) {
            return;
        }
        myDiamondsCountTv.setText(objBean.getAmount());
    }

    @Override
    public void showDiamondHistoryInfo(DiamondsHistroyInfoEntity diamondsHistroyInfoEntity) {
        //添加到显示集合内
        List<DiamondsHistroyInfoEntity.ObjBean.RecordsBean> newRecordsBeansList = diamondsHistroyInfoEntity.getObj().getRecords();
        if (!CollectionUtil.isEmpty(newRecordsBeansList)) {
            noDiamondsHistoryToastTv.setVisibility(View.GONE);
            diamondHistroyInfoSrlat.setVisibility(View.VISIBLE);
            if (clearFlag) {
                recordsBeansList.clear();
            }
            //设置数据并刷新adapter
            recordsBeansList.addAll(newRecordsBeansList);
            diamondsHistroyInfoAdapter.notifyDataSetChanged();
            //刷新handler
            Message msg = handlerForRefresh.obtainMessage();
            msg.what = 0x93;
            handlerForRefresh.sendMessage(msg);
            if (recordsBeansList.size() < pageSize) {
                ToastUtil.showBottomtoast("已显示全部");
            } else {
                pageNo = pageNo + 1;
            }
        } else {
            if (!CollectionUtil.isEmpty(recordsBeansList)) {
                ToastUtil.showBottomtoast("已显示全部");
            }else {
                noDiamondsHistoryToastTv.setVisibility(View.VISIBLE);
                diamondHistroyInfoSrlat.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void loadDiamondList(int pageNo, int pageSize) {
        requestArgsMap.clear();
        requestArgsMap.put("pageNo", pageNo);
        requestArgsMap.put("pageSize", pageSize);
        requestArgsMap.put("orderBy", "create_time");
        requestArgsMap.put("order", "desc");
        showLoading();
        presenter.request(requestArgsMap);
    }

    @Override
    public void onRefresh() {
        clearFlag = true;
        pageNo = 1;
        loadDiamondList(pageNo, pageSize);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
            presenter = null;
        }
        if (null != recordsBeansList) {
            recordsBeansList.clear();
            recordsBeansList = null;
        }
        if (diamondsHistroyInfoAdapter != null) {
            diamondsHistroyInfoAdapter = null;
        }
        if (null != linearLayoutManager) {
            linearLayoutManager = null;
        }
        if (null != handlerForRefresh) {
            handlerForRefresh = null;
        }
    }
}
